<?php
namespace app\api\forms\elements;

use Phalcon\Mvc\Model;
use Phalcon\Tag;
use Phalcon\Forms\Element;

class SelectUlElement extends Element
{

    public function render($attributes = null)
    {
        $html = '';

        $parameters = $this->prepareAttributes($attributes);

        $useOther = $this->getUserOption('use_other',false);

        if($useOther) {
            $html .= $this->getHidden();
            $parameters['id'] = $parameters[0] . '_show';
            unset($parameters[0]);
        }

        $html .= Tag::textField($parameters);
        $html .='<div class="caret_vessel"><span class="caret"></span></div>';
        $html .=$this->getUl();

        return $html;
    }

    protected function getHidden(){

        $hidden = Tag::hiddenField([
            $this->getName(),
            'value'=>$this->getValue(),
        ]);
        return $hidden;
    }

    protected function getUl(){
        $models = $this->getUserOption('models',[]);
        $using = $this->getUserOption('using',['name','name']);
        $html = '<ul class="form-groupar_ul">';
        foreach($models as $model){
            $html.='<li key="'.$model->$using[0].'">'.$model->$using[1].'</li>';
        }
        $html .='</ul>';
        return $html;
    }
}